<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_top">
        <div class="admin_main_block_select">
          <div class="input">
            <el-date-picker v-model="dataForm.create_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
            </el-date-picker>
          </div>
          <div class="input">
            <el-button icon="Search" @click="getDataList(true)">
              {{ i18n('common.search') }}
            </el-button>
          </div>
        </div>
      </div>
      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" stripe>
          <el-table-column :label="i18n('common.id')" width="70">
            <template v-slot="scope">
              <span v-if="scope.row.course">{{ scope.row.course.id }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="$t('course.category_id')" width="80">
            <template v-slot="scope">
              <span v-if="scope.row.course && scope.row.course.category">
                {{ scope.row.course.category.title }}
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('course.info')">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.course.picture" loading="lazy" lazy>
                    <template #error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ i18n('course.title') }}： {{ scope.row.course.title }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ i18n('course.create_time') }}： {{ scope.row.course.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('course.type')" width="80">
            <template v-slot="scope">
              <span v-if="scope.row.course">
                {{ scope.row.course.type.text }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="money" :label="$t('course.money')" width="80">
            <template v-slot="scope">
              <span v-if="scope.row.course">
                {{ scope.row.course.money }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('common.join_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="100">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:education:course:view')"
                type="info"
                icon="View"
                @click="router.push({ name: 'module_education_course_view', query: { id: scope.row.course.id } })">
                {{ i18n('common.view') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-course">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, query, isAuth } = useBase()

  const model = ref('organization/course')

  const categoryList = ref([])
  const typeList = ref([])

  const dataForm = reactive([
    'organization_id',
    'create_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    getDataList
  } = useTable(model.value, dataForm)

  const loadCategoryList = () => {
    $http({
      url: $http.adornUrl('/education/category/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        categoryList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  const loadTypeList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'education_course_type',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        typeList.value = data.data
      } else {
        $message.error(i18n(data.message))
      }
    })
  }

  onMounted(() => {
    dataForm.organization_id = query.id

    getDataList(model)

    loadCategoryList()
    loadTypeList()
  })
</script>

<style lang="scss" scoped>
  .preview {
    width: 50px;
    height: 50px;
  }
</style>
